﻿using Project.API.Models;
using Project.BLL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Project.API.Controllers
{
    [ApiAuthorize]
    public class RolesController : ApiController
    {
        Entities model=new Entities();
        public ResultData<List<RolesDto>> Get()
        {
            int count = 0;
            var list = new RolesBll().Query();

            var data = from r in list
                       select new RolesDto
                       {
                           RoleID = r.RId,
                           RoleName = r.RName,
                           DeptID = r.DId,
                           DeptName = r.Department.DName,
                           Remark = r.Remark,
                           Type = r.Type,
                           LastTime = r.LastTime,
                       };


            ResultData<List<RolesDto>> result = new ResultData< List<RolesDto>>();
            result.code = 0;
            result.msg = "ok";
            result.data = data.ToList();

            return result;
        }

        [Route("api/Roles/FuMenus")]
        public ResultData<List<MenusDto>> GetFu()
        {
            List<Menus> list = model.Menus.Where(m => m.ParentId == 0).ToList();

            var data = from m in list
                       select new MenusDto
                       {
                           MenuID = m.MId,
                           MenuName = m.MName,
                           Url = m.URL,
                           ParentID = m.ParentId,
                           Icon = m.Icon,
                           LastTime = m.LastTime,
                       };

            ResultData<List<MenusDto>> result = new ResultData<List<MenusDto>>();
            result.code = 0;
            result.msg = "ok";
            result.data = data.ToList();

            return result;
        }

        [Route("api/Roles/ZiMenus")]
        public ResultData<List<MenusDto>> GetZi(int id)
        {
            List<Menus> list = model.Menus.Where(m => m.ParentId == id).ToList();

            var data = from m in list
                       select new MenusDto
                       {
                           MenuID = m.MId,
                           MenuName = m.MName,
                           Url = m.URL,
                           ParentID = m.ParentId,
                           Icon = m.Icon,
                           LastTime = m.LastTime,
                       };

            ResultData<List<MenusDto>> result = new ResultData<List<MenusDto>>();
            result.code = 0;
            result.msg = "ok";
            result.data = data.ToList();

            return result;
        }

        [Route("api/Roles/XZMenus")]
        public List<int> GetXZ(int id)
        {
           List<Authority> list = model.Authority.Where(a => a.RId == id).ToList();

           List<int> ids = new List<int>();

            var data = model.Menus;

            foreach (var item in list)
            {
                if (data.Where(m => m.MId == item.MId).FirstOrDefault().ParentId != 0) {
                    ids.Add(item.MId);
                } 
            }

            return ids;
        }

        [Route("api/Roles/SQ")]
        public Result PostSQ(int id, [FromBody] int[] ids)
        {
            var list = model.Authority.Where(a => a.RId == id);

            model.Authority.RemoveRange(list);

            model.SaveChanges();

            int count = 0;

            foreach (int mid in ids)
            {
                Authority authority = new Authority();
                authority.AId = 0;
                authority.RId = id;
                authority.MId = mid;
                authority.Sort = 0;
                authority.LastTime = DateTime.Now;

                model.Authority.Add(authority);

                count += model.SaveChanges();
            }

            Result result = new Result();
            result.code = count > 0 ? 1 : 0;
            result.msg = count > 0 ? "授权成功" : "授权失败";

            return result;
        }



        [Route("api/Roles/PageList")]
        public ResultPageList<RolesDto> Get(int page, int limit, string name = "")
        {
            int count = 0;
            var list = new RolesBll().GetRolesByPage(name, page, limit, out count);

            var data = from r in list
                       select new RolesDto
                       {
                           RoleID = r.RId,
                           RoleName = r.RName,
                           DeptID = r.DId,
                           DeptName = r.Department.DName,
                           Remark = r.Remark,
                           Type = r.Type,
                           LastTime = r.LastTime,
                       };

            ResultPageList<RolesDto> result = new ResultPageList<RolesDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = data.ToList();

            return result;
        }

        [Route("api/Roles/Dept")]
        public ResultData<List<DepartmentDto>> GetDept()
        {
            int count = 0;
            var list = model.Department;

            var data = from r in list
                       select new DepartmentDto
                       {
                           DId= r.DId,
                           DName = r.DName,
                           Remark = r.Remark,
                           Type = r.Type,
                           LastTime = r.LastTime,
                       };

            ResultData<List<DepartmentDto>> result = new ResultData<List<DepartmentDto>>();
            result.code = 0;
            result.msg = "ok";
            result.data = data.ToList();

            return result;
        }


        [Route("api/Roles/Add")]
        public Result Post([FromBody] RolesDto value)
        {
            Roles Role = new Roles();
            Role.RId = value.RoleID;
            Role.RName = value.RoleName;
            Role.DId = value.DeptID;
            Role.Remark = value.Remark;
            Role.Type = value.Type;
            Role.LastTime = value.LastTime;

            bool bol = new RolesBll().Add(Role);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "角色信息添加成功！" : "error";
            return result;
        }

        [Route("api/Roles/Edit")]
        public Result Put([FromBody] RolesDto value)
        {
            Roles Role = new Roles();
            Role.RId = value.RoleID;
            Role.RName = value.RoleName;
            Role.DId = value.DeptID;
            Role.Remark = value.Remark;
            Role.Type = value.Type;
            Role.LastTime = value.LastTime;

            bool bol = new RolesBll().UpdateRoles(Role);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "角色信息修改成功！" : "出错了！";
            return result;
        }

        public Result Delete(int id)
        {
            var list = model.Authority.Where(a => a.RId == id);

            model.Authority.RemoveRange(list);

            model.SaveChanges();


            bool bol = new RolesBll().DeleteRoles(id);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "角色信息删除成功！" : "出错了！";
            return result;
        }

        [HttpDelete]
        public Result Delete([FromBody] int[] value)
        {
            foreach (var id in value) {
                var list = model.Authority.Where(a => a.RId == id);

                model.Authority.RemoveRange(list);

                model.SaveChanges();
            }

            bool bol = new RolesBll().DeleteRoles(value);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "角色信息删除成功！" : "出错了！";
            return result;
        }

    }
}
